package com.cek.tdrive.travel;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;

import com.cek.tdrive.adapter.VehicleAdapter;
import com.cek.tdrive.base.TDriveBaseActivity;
import com.cek.tdrive.constant.AppConstant;
import com.cek.tdrive.home.HomeScreenActivity;
import com.cek.tdrive.R;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AddVehicle extends TDriveBaseActivity implements OnClickListener {
	private String array_spinner[];
	private Spinner model;
	private String fuel_array[];
	private Spinner fueltype;
	private EditText vehiclename;
	private EditText mileage;
	private Button addvehicle;
	private Context ctx;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myvehicle);
		model = (Spinner) findViewById(R.id.model);
		fueltype=(Spinner)findViewById(R.id.fueltype);
		initSpinner();
		
		vehiclename=(EditText)findViewById(R.id.vehiclename);
		mileage=(EditText)findViewById(R.id.mileage);
		addvehicle=(Button)findViewById(R.id.addvehicle);
		ctx=this;
		addvehicle.setOnClickListener(this);
		
	}

		@Override
		public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.addvehicle) {
		
			String vehiclenameStr=vehiclename.getEditableText().toString();
			String modelStr=(String) model.getSelectedItem();
			String mil=mileage.getEditableText().toString();
			String type=(String) fueltype.getSelectedItem();
			SharedPreferences pref = getSharedPreferences(AppConstant.PREFS_NAME,0);
			String uName = pref.getString("username", null);
			addvehicle(uName,vehiclenameStr,modelStr,mil,type);
		}
			
	
	
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
	}
	public void initSpinner(){
		array_spinner=new String[20];
        array_spinner[0]="1994";
        array_spinner[1]="1995";
        array_spinner[2]="1996";
        array_spinner[3]="1997";
        array_spinner[4]="1998";
        array_spinner[5]="1999";
        array_spinner[6]="2000";
        array_spinner[7]="2001";
        array_spinner[8]="2002";
        array_spinner[9]="2003";
        array_spinner[10]="2004";
        array_spinner[11]="2005";
        array_spinner[12]="2006";
        array_spinner[13]="2007";
        array_spinner[14]="2008"; 
        array_spinner[15]="2009";
        array_spinner[16]="2010";
        array_spinner[17]="2011";
        array_spinner[18]="2012";
        array_spinner[19]="2013";
        
        fuel_array=new String[2];
        fuel_array[0]="Petrol";
        fuel_array[1]="Diesel";
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
		ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, array_spinner);
        model.setAdapter(adapter);
        ArrayAdapter adapter2 = new ArrayAdapter(this,android.R.layout.simple_spinner_item, fuel_array);
        fueltype.setAdapter(adapter2);
	}
	
		

	public boolean addvehicle(String uname, String vehiclename, String model,String mileage, String type) {
		System.out.println("vehiclename="+vehiclename);
		System.out.println("model="+model);
		System.out.println("mileage="+mileage);

		String Addvehicle=AppConstant.ADD_VEHICLE+"?emailid="+uname+"&vhname="+vehiclename+"&model="+model+"&mil="+mileage+"&type="+type+"&method=add";
		System.out.println("Addvehicle="+Addvehicle);
		HttpGet request= new HttpGet(Addvehicle);
		try { 
			HttpResponse  response=AppConstant.HTTP_CLIENT.execute(request);
			HttpEntity entity = response.getEntity();
			String respStr = EntityUtils.toString(entity);
			System.out.println("respStr = "+respStr);
			if(respStr.contains("Added")) {
				Toast.makeText(ctx,"Vehicle added successfully",Toast.LENGTH_SHORT).show();
				finish();
			}
			else {
				Toast.makeText(ctx, respStr, Toast.LENGTH_SHORT).show();
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) { 
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return false;	
		}
	}



